﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using XSocketServerLib;
namespace FileDownloadServer
{
    class Program
    {
        static string PATH = System.IO.Directory.GetCurrentDirectory()+ "\\player";
        static void Main(string[] args)
        {
            Log2Txt l = new Log2Txt();
            sock = new XSocketServer(2010, IPAddress.Parse("192.168.0.100"));
            sock.Configuration.AllowMutipleConnection = true;
            sock.XSocketRecive += new XSocketServer.XSocketReciveHandler(sock_XSocketRecive);
            sock.Start();
            Console.ReadLine();
        }

        static void sock_XSocketRecive(object sender, XSocketReciveEventArgs args)
        {
            MSG msg = (MSG)args.Data[0];
            switch (msg)
            {
                case MSG.REQUEST_DOWNLOAD:
                    ProcessREQUEST_DOWNLOAD(args);
                    break;
                
                
            }
        }
        static void ProcessREQUEST_DOWNLOAD(XSocketReciveEventArgs args)
        {
            try
            {
                int id = BitConverter.ToInt32(args.Data, 1);
                Request[args.Name] = id;
                FileInfo f = new FileInfo(PATH + "\\_" + id.ToString() + ".bin");
                List<byte> send_size = new List<byte>(BitConverter.GetBytes(f.Length));

                send_size.Insert(0, (byte)MSG.FILE_SIZE);
                sock.Send(args.Name, send_size.ToArray());

                sock.Send(args.Name, new byte[]{(byte)MSG.BEGIN_SEND});

                sock.Send(args.Name, f.FullName);
                //using (FileStream fs = f.Open(FileMode.Open))
                //{
                //    byte[] buffer = new byte[1024];
                //    int readed=-1;
                //    do
                //    {
                //        readed = fs.Read(buffer, 0, 1024);
                //        if (readed == 1024)
                //        {
                //            sock.Send(args.Name, buffer);
                //        }
                //        else
                //        {
                //            byte[] new_s = new byte[readed];
                //            Buffer.BlockCopy(buffer, 0, new_s, 0, readed);
                //            sock.Send(args.Name, new_s);
                //        }
                //    }
                //    while (readed != 0);
                //    Console.WriteLine("Send File " + id.ToString() + " Complete To " + args.Name);
                //}

                Console.WriteLine("Send File " + id.ToString() + " Complete To " + args.Name);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static Dictionary<string, bool> State= new Dictionary<string,bool>();
        static Dictionary<string, int> Request= new Dictionary<string,int>();
        static Dictionary<string, int> Downloaded = new Dictionary<string,int>();
        static XSocketServer sock;
    }
}
